package com.denlaku.longan.service.impl;

import com.denlaku.longan.qo.UserQuery;
import com.denlaku.longan.repository.UserRepository;
import com.denlaku.longan.service.UserService;
import com.denlaku.longan.util.BaseUtils;
import com.denlaku.longan.vo.User;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author tianx
 */
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserRepository userRepository;

	@Autowired
	private StringEncryptor stringEncryptor;

	@Override
	public User get(Long id) {
		UserQuery userQuery = new UserQuery();
		userQuery.setId(id);
		return userRepository.get(userQuery);
	}

	@Override
	public String getPassword(String account) {
		UserQuery userQuery = new UserQuery();
		userQuery.setAccount(account);
		userQuery.setStatus("1");
		String password = userRepository.getPassword(userQuery);
		return stringEncryptor.decrypt(password);
	}

	@Override
	public User getByAccount(String account) {
		UserQuery userQuery = new UserQuery();
		userQuery.setAccount(account);
		return userRepository.get(userQuery);
	}

	@Override
	public User get(String account, String password) {
		UserQuery userQuery = new UserQuery();
		userQuery.setAccount(account);
		userQuery.setPassword(encodePwd(account, password));
		userQuery.setStatus("1");
		return userRepository.get(userQuery);
	}

	@Override
	public List<User> list(UserQuery userQuery) {
		return userRepository.list(userQuery);
	}

	@Override
	public List<User> search(UserQuery userQuery) {
		return userRepository.search(userQuery);
	}

	@Override
	public Map<Long, User> listBaseInfoMap(Set<Long> userIds) {
		if (CollectionUtils.isEmpty(userIds)) {
			return Collections.emptyMap();
		}
		UserQuery userQuery = new UserQuery();
		userQuery.setIds(userIds);
		List<User> users = userRepository.listBaseInfo(userQuery);
		return users.stream().collect(Collectors.toMap(User::getId, Function.identity()));
	}

	@Override
	public void disabled(Long id) {
		User user = new User();
		user.setId(id);
		userRepository.disabled(user);
	}

	@Override
	public void enabled(Long id) {
		User user = new User();
		user.setId(id);
		userRepository.enabled(user);
	}

	@Override
	public void update(User user) {
		encodePwd(user);
		userRepository.update(user);
	}

	@Override
	public void delete(Long id) {
		userRepository.delete(id);
	}

	@Override
	public void add(User user) {
		encodePwd(user);
		userRepository.add(user);
	}

	private void encodePwd(User user) {
		String pwd = encodePwd(user.getAccount(), user.getPassword());
		user.setPassword(pwd);
	}

	private String encodePwd(String account, String password) {
		if (StringUtils.isNotEmpty(password)) {
			return BaseUtils.encodeSha256(password + ":" + account);
		}
		return null;
	}

}
